/** @file    EncodersXmlQt_Entertainment.cpp
 *  @author  Alessandro Polo
 *  @version $Id: EncodersXmlQt_Entertainment.cpp 2889 2010-08-11 21:53:50Z alex $
 *  @brief
 * The header for this class can be found in EncodersXmlQt_Entertainment.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/
 
 #include <framework/persistence/qt/EncodersXmlQt_Entertainment.h>

 #include <framework/persistence/qt/EncodersXmlQt_Core.h>
 #include <core/ObjectAllocator.h>


namespace wosh {
 namespace persistence {
  namespace qt {

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 WOSH_ENCODER_XML_Qt_BIND(wosh::persistence::qt::EncoderXml_Qt_Entertainment_MediaList, "wosh::entertainment::MediaList", 0.0, _static_EncoderXml_Qt_Entertainment_MediaList )

WRESULT EncoderXml_Qt_Entertainment_MediaList::serializeTo( const wosh::entertainment::MediaList* object, QDomDocument& doc, QDomElement& contNode, int encoder_options ) {
	if ( object == NULL ) return WRET_ERR_PARAM;
	WRESULT ret = EncoderXml_Qt_Object::serializeTo(object, doc, contNode, encoder_options );
	QDomElement stateNode = doc.createElement("state"); contNode.appendChild(stateNode);
	EncoderBaseXmlQt::serializeLong( "index", object->getCurrentIndex(), doc, stateNode, true );
	EncoderBaseXmlQt::serializeLong( "position", object->getCurrentPosition(), doc, stateNode, true );

	QDomElement tracksNode = doc.createElement("tracks"); contNode.appendChild(tracksNode);
	ret += EncoderBaseXmlQt::serializeListT<wosh::entertainment::MediaTrack,EncoderXml_Qt_Entertainment_MediaTrack>( object->getTracks(), doc, tracksNode, encoder_options );
// BUG! se extension of MediaTrack
	if ( isCompact(encoder_options) || !object->getProperties().isEmpty() ) {
		QDomElement node_properties = doc.createElement("properties"); contNode.appendChild(node_properties);
		ret += EncoderXml_Qt_PropertiesProvider::serializeTo(&object->getProperties(), doc, node_properties, encoder_options );
	 }
	return ret;
 }

WRESULT EncoderXml_Qt_Entertainment_MediaList::deserializeFrom( wosh::entertainment::MediaList* object, const QDomElement& contNode ) {
	if ( object == NULL ) return WRET_ERR_PARAM;
	WRESULT ret = EncoderXml_Qt_Object::deserializeFrom(object, contNode );
	QDomElement stateNode = contNode.namedItem("state").toElement();
	if ( !stateNode.isNull() ) {
		object->setCurrentIndex( EncoderBaseXmlQt::deserializeLong( "index", 0, contNode ) );
		object->setCurrentPosition( EncoderBaseXmlQt::deserializeLong( "position", 0, contNode ) );
	 }
	QDomElement tracksNode = contNode.namedItem("tracks").toElement();
	if ( !tracksNode.isNull() ) {
		ret += EncoderBaseXmlQt::deserializeListT<wosh::entertainment::MediaTrack,EncoderXml_Qt_Entertainment_MediaTrack>( object->getTracks(), tracksNode );
	 }
	QDomElement node_properties = contNode.namedItem("properties").toElement();
	if ( !node_properties.isNull() )
		ret += EncoderXml_Qt_PropertiesProvider::deserializeFrom(&object->getProperties(), node_properties );
	return ret;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 WOSH_ENCODER_XML_Qt_BIND(wosh::persistence::qt::EncoderXml_Qt_Entertainment_MediaTrack, "wosh::entertainment::MediaTrack", 0.0, _static_EncoderXml_Qt_Entertainment_MediaTrack )

WRESULT EncoderXml_Qt_Entertainment_MediaTrack::serializeTo( const wosh::entertainment::MediaTrack* object, QDomDocument& doc, QDomElement& contNode, int encoder_options ) {
	if ( object == NULL ) return WRET_ERR_PARAM;
	WRESULT ret = EncoderXml_Qt_Object::serializeTo( (const Object*)object, doc, contNode, encoder_options );
	EncoderBaseXmlQt::serializeLong( "index", object->getIndex(), doc, contNode, encoder_options );
	if ( isCompact(encoder_options) )
		contNode.setAttribute("resource", object->getTrack().getPathFull().c_str() );
	else {
		QDomElement resource_node = doc.createElement("resource"); contNode.appendChild(resource_node);
		ret += EncoderXml_Qt_ResourcePath::serializeTo(&object->getTrack(), doc, resource_node, true );
	 }
	if ( isCompact(encoder_options) || !object->getProperties().isEmpty() ) {
		QDomElement node_properties = doc.createElement("properties"); contNode.appendChild(node_properties);
		ret += EncoderXml_Qt_PropertiesProvider::serializeTo(&object->getProperties(), doc, node_properties, encoder_options );
	 }
	return ret;
 }

WRESULT EncoderXml_Qt_Entertainment_MediaTrack::deserializeFrom( wosh::entertainment::MediaTrack* object, const QDomElement& contNode ) {
	if ( object == NULL ) return WRET_ERR_PARAM;
	WRESULT ret = EncoderXml_Qt_Object::deserializeFrom( (Object*)object, contNode );
	object->setIndex( EncoderBaseXmlQt::deserializeLong( "index", -1, contNode ) );
	QDomElement resource_node = contNode.namedItem("resource").toElement();
	if ( !resource_node.isNull() )
		ret += EncoderXml_Qt_ResourcePath::deserializeFrom(&object->getTrack(), resource_node );
	else if ( contNode.hasAttribute("resource") )
		object->getTrack().set( contNode.attribute("resource").toLatin1().data() );
	QDomElement node_properties = contNode.namedItem("properties").toElement();
	if ( !node_properties.isNull() )
		ret += EncoderXml_Qt_PropertiesProvider::deserializeFrom(&object->getProperties(), node_properties );
	return ret;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 WOSH_ENCODER_XML_Qt_BIND(wosh::persistence::qt::EncoderXml_Qt_Entertainment_MediaZone, "wosh::entertainment::MediaZone", 0.0, _static_EncoderXml_Qt_Entertainment_MediaZone )

WRESULT EncoderXml_Qt_Entertainment_MediaZone::serializeTo( const wosh::entertainment::MediaZone* object, QDomDocument& doc, QDomElement& contNode, int encoder_options ) {
	if ( object == NULL ) return WRET_ERR_PARAM;
	WRESULT ret = EncoderXml_Qt_Object::serializeTo(object, doc, contNode, encoder_options );

	if ( isCompact(encoder_options) || !object->getProperties().isEmpty() ) {
		QDomElement node_properties = doc.createElement("properties"); contNode.appendChild(node_properties);
		ret += EncoderXml_Qt_PropertiesProvider::serializeTo(&object->getProperties(), doc, node_properties, encoder_options );
	 }
	return ret;
 }

WRESULT EncoderXml_Qt_Entertainment_MediaZone::deserializeFrom( wosh::entertainment::MediaZone* object, const QDomElement& contNode ) {
	if ( object == NULL ) return WRET_ERR_PARAM;
	WRESULT ret = EncoderXml_Qt_Object::deserializeFrom(object, contNode );

	QDomElement node_properties = contNode.namedItem("properties").toElement();
	if ( !node_properties.isNull() )
		ret += EncoderXml_Qt_PropertiesProvider::deserializeFrom(&object->getProperties(), node_properties );
	return ret;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

  }; // namespace qt
 }; // namespace persistence
}; // namespace wosh
